package com.beyondsoft.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.beyondsoft.model.Book;
import com.beyondsoft.provider.DatabaseHelper;

public class BookDao {
	Context context;
	SQLiteOpenHelper helper;

	public BookDao(Context context) {
		super();
		this.context = context;
	}

	public void insert(Book book) {
		helper = new DatabaseHelper(context);
		SQLiteDatabase database = helper.getWritableDatabase();
		String sql = "INSERT INTO books(name,author) VALUES(?,?)";
		database.execSQL(sql,
				new Object[] { book.getBook_name(), book.getAuthor() });
		database.close();
	}

	public Book find(String id) {
		Book book = new Book();
		helper = new DatabaseHelper(context);
		SQLiteDatabase database = helper.getWritableDatabase();
		String sql = "SELECT * FROM books WHERE id =?";
		Cursor cursor = database.rawQuery(sql, new String[] { id });
		if (cursor.getCount() < 1) {
			cursor.close();
			database.close();
			return null;
		}
		cursor.moveToFirst();
		int _id = cursor.getInt(cursor.getColumnIndexOrThrow("id"));
		String _name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
		String _author = cursor.getString(cursor
				.getColumnIndexOrThrow("author"));
		book.setId(_id);
		book.setBook_name(_name);
		book.setAuthor(_author);
		cursor.close();
		database.close();
		return book;

	}

	public List<Book> findAll() {
		helper = new DatabaseHelper(context);
		SQLiteDatabase database = helper.getWritableDatabase();
		String sql = "SELECT * FROM books";
		Book book;
		List<Book> books = new ArrayList<Book>();
		Cursor cursor = database.rawQuery(sql, new String[] {});
		if (cursor.getCount() < 1) {
			cursor.close();
			database.close();
			return null;
		}
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			book = new Book();
			String _id = cursor.getString(cursor.getColumnIndexOrThrow("id"));
			String _name = cursor.getString(cursor
					.getColumnIndexOrThrow("name"));
			String _author = cursor.getString(cursor
					.getColumnIndexOrThrow("author"));
			book.setId(Integer.parseInt(_id));
			book.setBook_name(_name);
			book.setAuthor(_author);
			books.add(book);
			cursor.moveToNext();
		}
		cursor.close();
		database.close();
		return books;
	}
	public boolean delete(String id){
		helper = new DatabaseHelper(context);
		SQLiteDatabase database = helper.getWritableDatabase();
		String sql = "DELETE FROM books WHERE id =?";
		database.beginTransaction();
		database.setTransactionSuccessful();
		database.endTransaction();
		database.execSQL(sql,
				new Object[] {id});
		database.close();
		return true;
	}
	public boolean update(Book book){
		helper = new DatabaseHelper(context);
		SQLiteDatabase database = helper.getWritableDatabase();
		String sql = "UPDATE books SET name =?, author =? WHERE id =?";
		database.execSQL(sql,
				new Object[] {book.getBook_name(), book.getAuthor(), book.getId()});
		database.close();
		return true;
	}
}
